import { Controller, Get, Post, Body, Patch, Param, Res, Delete } from '@nestjs/common';
import { DiscussService } from './discuss.service';


type Body = {
  user_id: string;
  content: string;
  food_id: String
}
@Controller('discuss')
export class DiscussController {
  constructor(private readonly discussService: DiscussService) { }


  @Post()
  async create(@Body() body: Body, @Res() res) {
    console.log(body)
    const result = this.discussService.create({ ...body, publish_time: Date.now() })
    if (!result) {
      res.status(426).send({
        message: "添加失败",
        status: 426
      })
    } else {
      res.status(200).send({
        message: "添加陈功",
        status: 200
      })
    }

  }
  @Get('/:id')
  async getcontent(@Param('id') param, @Res() res) {
    const result = await this.discussService.getcontent(param)
    if (!result) {
      res.status(500).send({
        message: "查询失败",
        status: 500
      })
    } else {
      res.status(200).send({
        data: result,
        message: "查询成功",
        status: 200
      })
    }
  }

  @Post('/admin/:skip/:Order')
  async getDiscussList(@Param() param, @Body() body) {
    console.log(body, '??')
    const result: any = await this.discussService.getDiscussList(Number(param.skip), param.Order, body.word)
    return {
      data: result.data,
      count: result.count,
      message: "查询成功",
      status: 200
    }
  }
  @Delete('/:id')
  async deleteOne(@Param() Param, @Res() res) {
    const result = await this.discussService.deleteOne(Param.id)
    if (!result) {
      res.status(427).send({
        status: 427,
        message: '删除失败'
      })

    } else {
      if (result.deletedCount == 1) {
        res.status(200).send({
          status: 200,
          message: '成功'
        })
      } else {
        res.status(427).send({
          status: 427,
          message: '删除失败'
        })
      }

    }
  }
}
